package com.baosight.risk.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通用http发送方法
 *
 * @author duanyongqiang
 */
@Slf4j
public class HttpUtils {
    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static String sendGet(String url) {
        HttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(entity, "UTF-8");
                if (content == null) {
                    content = "";
                }
                log.info(content);
                return content;
            }
        } catch (ClientProtocolException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            try {
                ((CloseableHttpClient) client).close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
        return "";
    }

    /**
     * 简单的http post请求("application/x-www-form-urlencoded")格式
     *
     * @param url
     * @param params
     * @return
     */
    public static String sendPost(String url, Map<String, Object> params) {
        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        //设置参数
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        for (Map.Entry<String, Object> e : params.entrySet()) {
            list.add(new BasicNameValuePair(e.getKey(), String.valueOf(e.getValue())));
        }
        try {
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
                post.setEntity(entity);
            }
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(entity, "UTF-8");
                if (content == null) {
                    content = "";
                }
                return content;
            }
        } catch (ClientProtocolException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            try {
                ((CloseableHttpClient) client).close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }

        return "";
    }


    /**
     * 简单的http post请求("application/json")格式
     *
     * @param url
     * @param params
     * @return
     */
    public static String sendJsonPost(String url, Map<String, Object> params) {
        HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        //设置参数
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        for (Map.Entry<String, Object> e : params.entrySet()) {
            list.add(new BasicNameValuePair(e.getKey(), String.valueOf(e.getValue())));
        }
        try {
            if (list.size() > 0) {
                StringEntity entity = new StringEntity(params.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                post.setEntity(entity);
            }
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(entity, "UTF-8");
                if (content == null) {
                    content = "";
                }
                return content;
            }
        } catch (ClientProtocolException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            try {
                ((CloseableHttpClient) client).close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }

        return "";
    }
}